//
//  ZHClinicStatisticsCollectionView.m
//  Doctor
//
//  Created by liuzhishan on 15/12/17.
//  Copyright © 2015年 shanshan. All rights reserved.
//

#import "ZHClinicStatisticsCollectionView.h"
#import "ZHStatisticsColumnarCollectionViewCell.h"
#import "ZHStatisticsThreeCollectionViewCell.h"
#import "ZHClinicAllData.h"
#import "ZHShowStatisticsMegView.h"
#define  IMAGEBGNAME  @"LineBg"
#define  HIGHTCOUNT  5
#define  HEIGHTGAP   60

@interface ZHClinicStatisticsCollectionView ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,retain) NSMutableArray *titleArray;

@property (nonatomic,retain) UICollectionView *myColletionView;

@property (nonatomic,strong) UIView *hideView;

@property (nonatomic,strong) ZHShowStatisticsMegView *showMegView;

@property (nonatomic,assign) CGFloat myCenterX;

@end

@implementation ZHClinicStatisticsCollectionView


- (instancetype)initWithFrame:(CGRect)frame
{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        
        [self addMYImageView];
        [self addMYCollection];
        [self addMyLables];
        [self addMyHideView];
        
        
    }
    return self;
}


-(void)addMyHideView
{
    _hideView = [[UIView alloc]init];
    
    _hideView.translatesAutoresizingMaskIntoConstraints = NO;
     [self addSubview:_hideView];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_hideView attribute:(NSLayoutAttributeLeft) relatedBy:(NSLayoutRelationEqual) toItem:self attribute:(NSLayoutAttributeLeft) multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_hideView attribute:(NSLayoutAttributeRight) relatedBy:(NSLayoutRelationEqual) toItem:self attribute:(NSLayoutAttributeRight) multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_hideView attribute:(NSLayoutAttributeTop) relatedBy:(NSLayoutRelationEqual) toItem:self attribute:(NSLayoutAttributeTop) multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_hideView attribute:(NSLayoutAttributeBottom) relatedBy:(NSLayoutRelationEqual) toItem:self attribute:(NSLayoutAttributeBottom) multiplier:1.0 constant:0]];
    
    
    _hideView.backgroundColor = [UIColor clearColor];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickHideView)];
    [_hideView addGestureRecognizer:tap];
    
    _hideView.hidden = YES;
    
   
}

-(void)clickHideView
{
    _showMegView.hidden = YES;
    _hideView.hidden = YES;
}


#pragma mark 添加背景图
-(void)addMYImageView
{
    UIImageView *imageView = [[UIImageView alloc]init];
    
    imageView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:imageView];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:(NSLayoutAttributeLeft) relatedBy:(NSLayoutRelationEqual) toItem:self attribute:(NSLayoutAttributeLeft) multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:(NSLayoutAttributeRight) relatedBy:(NSLayoutRelationEqual) toItem:self attribute:(NSLayoutAttributeRight) multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:(NSLayoutAttributeTop) relatedBy:(NSLayoutRelationEqual) toItem:self attribute:(NSLayoutAttributeTop) multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:(NSLayoutAttributeBottom) relatedBy:(NSLayoutRelationEqual) toItem:self attribute:(NSLayoutAttributeBottom) multiplier:1.0 constant:0]];
    
    imageView.image = [UIImage imageNamed:IMAGEBGNAME];
    
    
}





#pragma mark set方法初始化数组，刷新界面  初始化数组 左边数值

-(void)setDataArray:(NSMutableArray *)dataArray
{
    _dataArray  = dataArray;
    [self addMYLable];
    
    
    [self.myColletionView reloadData];
}



#pragma mark 添加lable
-(void)addMyLables
{
    
    for ( NSInteger i = 0 ;  i < HIGHTCOUNT ; i ++) {
        
        UILabel *lable = [[UILabel alloc]init];
        
        lable.textColor = [UIColor whiteColor];
        
        lable.textAlignment = NSTextAlignmentRight;
        lable.font = [UIFont systemFontOfSize:16 * WIDPro -2];
        
        [self addSubview:lable];
        
        lable.tag = 1000 + i;
        
        
    }
    
    
}

    
 
#pragma mark 修改Lable 值
-(void)addMYLable
{
    
    ZHSaveCommenMeg *meg = [ZHSaveClickDate shareData].judgeModel;
    
    CGFloat oneNubValue = meg.max / (HIGHTCOUNT - 1);
    CGFloat currentNubValue = 0;
    
    for ( NSInteger i = 0 ;  i < HIGHTCOUNT ; i ++) {
        
        UILabel *lable =  (UILabel*)[self viewWithTag:(1000+i)];
        
        
        currentNubValue = meg.max - i*oneNubValue;
        
        
        lable.text = [NSString stringWithFormat:@"%.2f",currentNubValue];
        
        if (meg.max >= 4) {
            
            lable.text = [NSString stringWithFormat:@"%.f",currentNubValue];
        }
        
        
        [lable setMyFramewithX:10 andY:15 + (i * HEIGHTGAP) andWidth:36 andHeight:20];
        
        
    }
    
}

#pragma  mark  添加Collection

-(void)addMYCollection
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    flowLayout.itemSize = CGSizeMake(WIDPro  * 78 , HEIPro *  270);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    
    flowLayout.sectionInset = UIEdgeInsetsMake(0,0, 0, 0);
    
    _myColletionView = [[UICollectionView alloc] initWithFrame: CGRectMake(50 *WIDPro,15*HEIPro, 780*WIDPro, HEIPro*270)collectionViewLayout:flowLayout];
    _myColletionView.backgroundColor = [UIColor clearColor];
    _myColletionView.bounces = NO;
    _myColletionView.delegate = self;
    _myColletionView.dataSource = self;
    
    
    
    [_myColletionView registerClass:[ZHStatisticsThreeCollectionViewCell class] forCellWithReuseIdentifier:@"ClinicLineCell"];
    
    
    [self addSubview:_myColletionView];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count ;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ZHStatisticsThreeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ClinicLineCell" forIndexPath:indexPath];
    
    [cell setDataWithArray:self.dataArray andIndex:indexPath.row];
    
    
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZHClinicAllData *model = [self.dataArray objectAtIndex:indexPath.row];
    
    if (_showMegView == nil) {
        
        self.showMegView = [[ZHShowStatisticsMegView alloc]init];
        self.showMegView.layer.masksToBounds = YES;
        self.showMegView.layer.cornerRadius =  10*WIDPro;
        
        [self.showMegView setMyBgAlpha:0.5];
        [self.showMegView setMyFramewithX:0 andY:0 andWidth:self.width/2 andHeight:self.height/4];
        self.showMegView.centerX = self.width/2;
        self.showMegView.centerY = self.height / 3;
        
        [self addSubview:_showMegView];
        
    }
    
    _showMegView.hidden = NO;
    _hideView.hidden = NO;
    
//    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
//    
//    CGRect rect = [self convertRect:collectionView.frame fromView:collectionView];
//    
//    
////    self.myCenterX = cell.centerX - collectionView.contentOffset.x - rect.origin.x;
////
////    
////    _showMegView.centerX = self.myCenterX ;
    
    [_showMegView setNotAppointArriveCount:(model.patienterNumber - model.notArrive - model.appointmentNumber) andAppointArriveCount:model.appointmentNumber andAppointNotArriveCount:model.notArrive];

    
    NSLog(@"%f",self.myCenterX);
    
}







@end
